# 09_jes2013_wave5.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2020-5-16 Taka-aki Asano
# Last Modified: 2021-10-9

# package
require("haven")
require("dplyr")
require("ltm")
require("plink")


# dataset
JES2013 <- read_sav(
  "JESV第5･8波2013年参院選郵送調査（事前事後統合）.sav", 
  user_na = FALSE
)


# respondent
JES2013_Respondent <- JES2013[,c("id", paste0("a", 18:27, "x1"))]
colnames(JES2013_Respondent)[-1] <- paste0("q", 18:27)


# IRT
## handle missing data
JES2013_Respondent <- JES2013_Respondent[rowSums(JES2013_Respondent[,-1], na.rm = TRUE) != 0,]

## estimate item parameters
irt2013 <- ltm::grm(JES2013_Respondent[,-1], IRT.param = FALSE)
irt2013 ## viewing

## estimate voters ideology
score2013_Respondent <- ltm::factor.scores(irt2013, JES2013_Respondent[,-1])


# rescale
## common item
common2012 <- c("q8", "q9", "q10", "q11", "q12", "q13", "q14")
common2013 <- c("q19", "q20", "q21", "q22", "q23", "q24", "q25")
index <- data.frame(
  group1 = match(common2012, colnames(JES2012_Respondent[,-1])), 
  group2 = match(common2013, colnames(JES2013_Respondent[,-1]))
)

## list of parameters
### 2012
res2012 <- coef(irt2012)
res2012 <- res2012[,c(4,1:3)]
colnames(res2012) <- c("a","b1","b2","b3")
### 2013
res2013 <- coef(irt2013)
res2013 <- res2013[,c(4,1:3)]
colnames(res2013) <- c("a","b1","b2","b3")
### merge
pm <- list(res2012, res2013)

## list of scale
rescat <- list(rep(4, nrow(res2012)), 
               rep(4, nrow(res2013)))

## as.irt.pars
pm2012 <- as.poly.mod(n = nrow(res2012), model = "grm", 
                      items = 1:nrow(res2012))
pm2013 <- as.poly.mod(n = nrow(res2013), model = "grm", 
                      items = 1:nrow(res2013))
res <- as.irt.pars(pm, common = index, cat = rescat, 
                   poly.mod = list(pm2012, pm2013), 
                   location = FALSE)

## list of voters' position
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2013_Respondent$score.dat$z1)

## rescale
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)
summary(plink_out)

## estimate voters ideology (rescale)
score2013_Respondent_plink <- link.ability(plink_out)$group2
JES2013_Respondent$Ideology <- -1 * score2013_Respondent_plink


# LDP's position
JES2013_LDP <- JES2013[,c("id", paste0("a", 18:27, "x3_1"))]
colnames(JES2013_LDP)[-1] <- paste0("q", 18:27)
JES2013_LDP <- JES2013_LDP[rowSums(JES2013_LDP[,-1], na.rm = TRUE) != 0,]
score2013_LDP <- ltm::factor.scores(irt2013, JES2013_LDP[,-1])
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2013_LDP$score.dat$z1)
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)
JES2013_LDP$Ideology <- -1 * link.ability(plink_out)$group2


# DPJ's position
JES2013_DPJ <- JES2013[,c("id", paste0("a", 18:27, "x3_2"))]
colnames(JES2013_DPJ)[-1] <- paste0("q", 18:27)
JES2013_DPJ <- JES2013_DPJ[rowSums(JES2013_DPJ[,-1], na.rm = TRUE) != 0,]
score2013_DPJ <- ltm::factor.scores(irt2013, JES2013_DPJ[,-1])
ideology <- list(score2012_Respondent$score.dat$z1, 
                 score2013_DPJ$score.dat$z1)
plink_out <- plink(res, common = index, base.grp = 1, 
                   rescale = "MS", ability = ideology)
JES2013_DPJ$Ideology <- -1 * link.ability(plink_out)$group2
